//
//  mm_codeItemForRepairItemViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-7-30.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_codeItemForRepairItemViewController.h"

@interface mm_codeItemForRepairItemViewController ()

@end

@implementation mm_codeItemForRepairItemViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.frame = CGRectMake(0, 0, 500, 600);
    
    [self initAllTheData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self addAllObserver];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self removeAllObserver];
}

- (void)viewDidUnload {
    [self setSymptomCodeTxt:nil];
    [self setFaultCodeTxt:nil];
    [self setActionCodeTxt:nil];
    [self setSymptomCodeDescriptionIndexBtn:nil];
    [self setSymptomCodeDescriptionItemBtn:nil];
    [self setSymptomCodeBtn:nil];
    [self setFaultCodeDescriptionItemBtn:nil];
    [self setFaultCodeBtn:nil];
    [self setActionCodeBtn:nil];
    [super viewDidUnload];
}

- (IBAction)symptomCodeDescriptionIndexBtnPressed:(UIButton *)sender
{
    //请求数据
    webService *web=[[webService alloc] init];
    web.methodName = MM_GET_SYMPTOM_CODE_DESCRIPTION_LIST_WEB_MSG_;
    [web commonGetSharedRequest:web.methodName wsparas:nil];
    
    //创建下拉菜单
    [self createThePopViewControl:sender];
}

- (IBAction)symptomCodeDescriptionItemBtnPressed:(UIButton *)sender
{
    //判断上一级是否选择，有选择，请求数据
    if (_nRowForSymptomCodeIndexSel == -1)
    {
        ;
    }
    else
    {
        //请求数据
        webService *web=[[webService alloc] init];
        web.methodName = MM_GET_SYMPTOM_CODE_DESCRIPTION_ITEM_LIST_WEB_MSG_;
        
        NSMutableArray *arrTmp = [NSMutableArray array];
        [arrTmp addObject:MM_SYMPTOM_CODE_DESCRIPTION_INDEX_LIST_SYMPTOM_CODE_DESCRIPTION_NUMBER_KEY_STR_];
        [arrTmp addObject:[[_arrOriginalDataForSymptomCodeIndex objectAtIndex:_nRowForSymptomCodeIndexSel] objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_INDEX_LIST_SYMPTOM_CODE_DESCRIPTION_NUMBER_KEY_STR_]];
        
        [web commonGetSharedRequest:web.methodName wsparas:arrTmp];
        
        //创建下拉菜单
        [self createThePopViewControl:sender];
    }
}

- (IBAction)symptomCodeBtnPressed:(UIButton *)sender
{
    
    //判断上一级是否选择，有选择，请求数据
    if (_nRowForSymPtomCodeDescriptionSel == -1)
    {
        ;
    }
    else
    {
        //请求数据
        webService *web=[[webService alloc] init];
        web.methodName = MM_GET_SYMPTOM_CODE_LIST_WEB_MSG_;
        
        NSMutableArray *arrTmp = [NSMutableArray array];
        [arrTmp addObject:MM_SYMPTOM_CODE_DESCRIPTION_INDEX_LIST_SYMPTOM_CODE_DESCRIPTION_NUMBER_KEY_STR_];
        [arrTmp addObject:[[_arrOriginalDataForSymptomCodeIndex objectAtIndex:_nRowForSymptomCodeIndexSel] objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_INDEX_LIST_SYMPTOM_CODE_DESCRIPTION_NUMBER_KEY_STR_]];
        [arrTmp addObject:MM_SYMPTOM_CODE_DESCRIPTION_ITEM_SYMPTOM_CODE_DESCRIPTION_ITEM_NUMBER_KEY_STR_];
        [arrTmp addObject:[[_arrOriginalDataForSymptomCodeDescription objectAtIndex:_nRowForSymPtomCodeDescriptionSel] objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_ITEM_SYMPTOM_CODE_DESCRIPTION_ITEM_NUMBER_KEY_STR_]];
        
        [web commonGetSharedRequest:web.methodName wsparas:arrTmp];
        
        //创建下拉菜单
        [self createThePopViewControl:sender];
    }
}

- (IBAction)faultCodeDescriptionItemBtnPressed:(UIButton *)sender
{
    //请求数据
    webService *web=[[webService alloc] init];
    web.methodName = MM_GET_FAULT_CODE_DESCRIPTION_ITEM_LIST_WEB_MSG_;
    [web commonGetSharedRequest:web.methodName wsparas:nil];
    
    //创建下拉菜单
    [self createThePopViewControl:sender];
}

- (IBAction)faultCodeBtnPressed:(UIButton *)sender
{
    //判断上一级是否选择，有选择，请求数据
    if (_nRowForFaultCodeDescription == -1)
    {
        ;
    }
    else
    {
        //请求数据
        webService *web=[[webService alloc] init];
        web.methodName = MM_GET_FAULT_CODE_LIST_WEB_MSG_;
        
        NSMutableArray *arrTmp = [NSMutableArray array];
        [arrTmp addObject:MM_FAULT_CODE_DESCRIPTION_ITEM_LIST_FAULT_CODE_DESCRIPTION_ITEM_NUMBER_KEY_STR_];
        [arrTmp addObject:[[_arrOriginalDataForFaultCodeDescription objectAtIndex:_nRowForFaultCodeDescription] objectForKey:MM_FAULT_CODE_DESCRIPTION_ITEM_LIST_FAULT_CODE_DESCRIPTION_ITEM_NUMBER_KEY_STR_]];
        
        [web commonGetSharedRequest:web.methodName wsparas:arrTmp];
        
        //创建下拉菜单
        [self createThePopViewControl:sender];
    }
}

- (IBAction)actionCodeBtnPressed:(UIButton *)sender
{
    //请求数据
    webService *web=[[webService alloc] init];
    web.methodName = MM_GET_ACTION_CODE_LIST_WEB_MSG_;
    [web commonGetSharedRequest:web.methodName wsparas:nil];
    
    //创建下拉菜单
    [self createThePopViewControl:sender];
}

//接口的回调方法
-(void)getSymptomCodeDescriptionIndexList:(NSNotification*)notification
{
    //更新数据
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    _arrOriginalDataForSymptomCodeIndex = [theJson objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_INDEX_LIST_KEY_STR_];
    _arrDisDataForSymptomCodeIndex = [NSMutableArray arrayWithCapacity:[_arrOriginalDataForSymptomCodeIndex count]];
    for (int i = 0; i<[_arrOriginalDataForSymptomCodeIndex count]; i++)
    {
        NSString *strTmp = [[_arrOriginalDataForSymptomCodeIndex objectAtIndex:i] objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_INDEX_LIST_SYMPTOM_CODE_DESCRIPTION_NUMBER_KEY_STR_];
        strTmp = [strTmp stringByAppendingFormat:@"-%@",[[_arrOriginalDataForSymptomCodeIndex objectAtIndex:i] objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_INDEX_LIST_SYMPTOM_CODE_DESCRIPTION_KEY_STR_]];
        [_arrDisDataForSymptomCodeIndex addObject:strTmp];
    }
    
    //刷新控件内容
    _contentCon.list = _arrDisDataForSymptomCodeIndex;
    [_contentCon.myTableView reloadData];
}

-(void)getSymptomCodeDescriptionItemList:(NSNotification*)notification
{
    //更新数据
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    _arrOriginalDataForSymptomCodeDescription = [theJson objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_ITEM_LIST_KEY_STR_];
    _arrDisDataForSymptomCodeDescription = [NSMutableArray arrayWithCapacity:[_arrOriginalDataForSymptomCodeDescription count]];
    for (int i = 0; i<[_arrOriginalDataForSymptomCodeDescription count]; i++)
    {
        NSString *strTmp = [[_arrOriginalDataForSymptomCodeDescription objectAtIndex:i] objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_ITEM_SYMPTOM_CODE_DESCRIPTION_ITEM_NUMBER_KEY_STR_];
        strTmp = [strTmp stringByAppendingFormat:@"-%@",[[_arrOriginalDataForSymptomCodeDescription objectAtIndex:i] objectForKey:MM_SYMPTOM_CODE_DESCRIPTION_ITEM_SYMPTOM_CODE_DESCRIPTION_ITEM_KEY_STR_]];
        [_arrDisDataForSymptomCodeDescription addObject:strTmp];
    }
    
    //刷新控件内容
    _contentCon.list = _arrDisDataForSymptomCodeDescription;
    [_contentCon.myTableView reloadData];
}

-(void)getSymptomCodeList:(NSNotification*)notification
{
    //更新数据
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    _arrOriginalDataForSymptom = [theJson objectForKey:MM_SYMPTOM_CODE_LIST_KEY_STR_];
    _arrDisDataForSymptom = [NSMutableArray arrayWithCapacity:[_arrOriginalDataForSymptom count]];
    for (int i = 0; i<[_arrOriginalDataForSymptom count]; i++)
    {
        NSString *strTmp = [[_arrOriginalDataForSymptom objectAtIndex:i] objectForKey:MM_SYMPTOM_CODE_LIST_SYMPTOM_CODE_KEY_STR_];
        strTmp = [strTmp stringByAppendingFormat:@"-%@",[[_arrOriginalDataForSymptom objectAtIndex:i] objectForKey:MM_SYMPTOM_CODE_LIST_SYMPTOM_KEY_STR_]];
        [_arrDisDataForSymptom addObject:strTmp];
    }
    
    //刷新控件内容
    _contentCon.list = _arrDisDataForSymptom;
    [_contentCon.myTableView reloadData];
}

-(void)getFaultCodeDescriptionItemList:(NSNotification*)notification
{
    //更新数据
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    _arrOriginalDataForFaultCodeDescription = [theJson objectForKey:MM_FAULT_CODE_DESCRIPTION_ITEM_LIST_KEKY_STR_];
    _arrDisDataForFaultCodeDescription = [NSMutableArray arrayWithCapacity:[_arrOriginalDataForFaultCodeDescription count]];
    for (int i = 0; i<[_arrOriginalDataForFaultCodeDescription count]; i++)
    {
        NSString *strTmp = [[_arrOriginalDataForFaultCodeDescription objectAtIndex:i] objectForKey:MM_FAULT_CODE_DESCRIPTION_ITEM_LIST_FAULT_CODE_DESCRIPTION_ITEM_NUMBER_KEY_STR_];
        strTmp = [strTmp stringByAppendingFormat:@"-%@",[[_arrOriginalDataForFaultCodeDescription objectAtIndex:i] objectForKey:MM_FAULT_CODE_DESCRIPTION_ITEM_LIST_FAULT_CODE_DESCRIPTION_ITEM_KEY_STR_]];
        [_arrDisDataForFaultCodeDescription addObject:strTmp];
    }
    
    //刷新控件内容
    _contentCon.list = _arrDisDataForFaultCodeDescription;
    [_contentCon.myTableView reloadData];
}

-(void)getFaultCodeList:(NSNotification*)notification
{
    //更新数据
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    _arrOriginalDataForFaultCode = [theJson objectForKey:MM_FAULT_CODE_LIST_KEY_STR_];
    _arrDisDataForFaultCode = [NSMutableArray arrayWithCapacity:[_arrOriginalDataForFaultCode count]];
    for (int i = 0; i<[_arrOriginalDataForFaultCode count]; i++)
    {
        NSString *strTmp = [[_arrOriginalDataForFaultCode objectAtIndex:i] objectForKey:MM_FAULT_CODE_LIST_FAULT_CODE_NUMBER_KEY_STR_];
        strTmp = [strTmp stringByAppendingFormat:@"-%@",[[_arrOriginalDataForFaultCode objectAtIndex:i] objectForKey:MM_FAULT_CODE_LIST_FAULT_CODE_DESCRIPTION_KEY_STR_]];
        [_arrDisDataForFaultCode addObject:strTmp];
    }
    
    //刷新控件内容
    _contentCon.list = _arrDisDataForFaultCode;
    [_contentCon.myTableView reloadData];
}

-(void)getActionCodeList:(NSNotification*)notification
{
    //更新数据
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    _arrOriginalDataForActionCode = [theJson objectForKey:MM_ACTION_CODE_LIST_KEY_STR_];
    _arrDisDataForActionCode = [NSMutableArray arrayWithCapacity:[_arrOriginalDataForActionCode count]];
    for (int i = 0; i<[_arrOriginalDataForActionCode count]; i++)
    {
        NSString *strTmp = [[_arrOriginalDataForActionCode objectAtIndex:i] objectForKey:MM_ACTION_CODE_LIST_ACTION_CODE_KEY_STR_];
        strTmp = [strTmp stringByAppendingFormat:@"-%@",[[_arrOriginalDataForActionCode objectAtIndex:i] objectForKey:MM_ACTION_CODE_LIST_ACTION_CODE_DESCRIPTION_KEY_STR_]];
        [_arrDisDataForActionCode addObject:strTmp];
    }
    
    //刷新控件内容
    _contentCon.list = _arrDisDataForActionCode;
    [_contentCon.myTableView reloadData];
}

-(void)initAllTheData
{
    _arrOriginalDataForSymptomCodeIndex = nil;
    _arrDisDataForSymptomCodeIndex = nil;
    _nRowForSymptomCodeIndexSel = -1;
    
    _arrOriginalDataForSymptomCodeDescription = nil;
    _arrDisDataForSymptomCodeDescription = nil;
    _nRowForSymPtomCodeDescriptionSel = -1;
    
    _arrOriginalDataForSymptom = nil;
    _arrDisDataForSymptom = nil;
    _nRowForSymptom = -1;
    
    _arrOriginalDataForFaultCodeDescription = nil;
    _arrDisDataForFaultCodeDescription = nil;
    _nRowForFaultCodeDescription = -1;
    
    _arrOriginalDataForFaultCode = nil;
    _arrDisDataForFaultCode = nil;
    _nRowForFaultCode = -1;
    
    _arrOriginalDataForActionCode = nil;
    _arrDisDataForActionCode = nil;
    _nRowForActionCode = -1;
    
    self.symptomCodeTxt.text = @"";
    self.faultCodeTxt.text = @"";
    self.actionCodeTxt.text = @"";
}

//创建下拉菜单的回调方法
-(void)createThePopViewControl:(UIButton *)sender
{
    if (!_contentCon)
    {
        _contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
    }
    
    _contentCon.delegate = self;
    _contentCon.btnSender=sender;
    
    _popCon = [[UIPopoverController alloc] initWithContentViewController:_contentCon];
    [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:222 alpha:0.9]];
    [DDPopoverBackgroundView setArrowBase:30.0];//箭头
    [DDPopoverBackgroundView setArrowHeight:15.0];
    [DDPopoverBackgroundView setContentInset:1.0];
    
    [_popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
    [_popCon setPopoverContentSize:CGSizeMake(sender.frame.size.width, 280)];
    
    [_popCon presentPopoverFromRect:CGRectMake(40, 15, 70, 20) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

//下拉菜单的回调方法
-(void)selectDelegate:(NSInteger)row btn:(UIButton *)btn
{
    if (btn == self.symptomCodeDescriptionIndexBtn)
    {
        _nRowForSymptomCodeIndexSel = row;
        
        //清空数据
        _nRowForSymPtomCodeDescriptionSel = -1;
        _nRowForSymptom = -1;
        
        [self.symptomCodeDescriptionItemBtn setTitle:@"" forState:UIControlStateNormal];
        [self.symptomCodeBtn setTitle:@"" forState:UIControlStateNormal];
        self.symptomCodeTxt.text = @"";
        
        [MainMenuDataModel sharedMainMenuDataModel].strSymptomCode = @"";
    }
    else if (btn == self.symptomCodeDescriptionItemBtn)
    {
        _nRowForSymPtomCodeDescriptionSel = row;
        
        //清空数据
        _nRowForSymptom = -1;
        
        [self.symptomCodeBtn setTitle:@"" forState:UIControlStateNormal];
        self.symptomCodeTxt.text = @"";
        
        [MainMenuDataModel sharedMainMenuDataModel].strSymptomCode = @"";
    }
    else if (btn == self.symptomCodeBtn)
    {
        _nRowForSymptom = row;
        
        //赋值
        self.symptomCodeTxt.text = [[_arrOriginalDataForSymptom objectAtIndex:_nRowForSymptom] objectForKey:MM_SYMPTOM_CODE_LIST_SYMPTOM_CODE_KEY_STR_];
        
        [MainMenuDataModel sharedMainMenuDataModel].strSymptomCode = self.symptomCodeTxt.text;
    }
    else if (btn == self.faultCodeDescriptionItemBtn)
    {
        _nRowForFaultCodeDescription = row;
        
        //清空数据
        [self.faultCodeBtn setTitle:@"" forState:UIControlStateNormal];
        self.faultCodeTxt.text = @"";
        
        [MainMenuDataModel sharedMainMenuDataModel].strFaultCode = @"";
    }
    else if (btn == self.faultCodeBtn)
    {
        _nRowForFaultCode = row;
        
        //赋值
        self.faultCodeTxt.text = [[_arrOriginalDataForFaultCode objectAtIndex:_nRowForFaultCode] objectForKey:MM_FAULT_CODE_LIST_FAULT_CODE_NUMBER_KEY_STR_];
                
        [MainMenuDataModel sharedMainMenuDataModel].strFaultCode = self.faultCodeTxt.text;
    }
    else if (btn == self.actionCodeBtn)
    {
        _nRowForActionCode = row;
        
        //赋值
        self.actionCodeTxt.text = [[_arrOriginalDataForActionCode objectAtIndex:_nRowForActionCode] objectForKey:MM_ACTION_CODE_LIST_ACTION_CODE_KEY_STR_];
        
        [MainMenuDataModel sharedMainMenuDataModel].strActionCode = self.actionCodeTxt.text;
    }
    
    [_popCon dismissPopoverAnimated:YES];
    _popCon = nil;
}

//注册消息
-(void)addAllObserver
{
    //注册消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSymptomCodeDescriptionIndexList:) name:MM_GET_SYMPTOM_CODE_DESCRIPTION_LIST_WEB_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSymptomCodeDescriptionItemList:) name:MM_GET_SYMPTOM_CODE_DESCRIPTION_ITEM_LIST_WEB_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSymptomCodeList:) name:MM_GET_SYMPTOM_CODE_LIST_WEB_MSG_ object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getFaultCodeDescriptionItemList:) name:MM_GET_FAULT_CODE_DESCRIPTION_ITEM_LIST_WEB_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getFaultCodeList:) name:MM_GET_FAULT_CODE_LIST_WEB_MSG_ object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getActionCodeList:) name:MM_GET_ACTION_CODE_LIST_WEB_MSG_ object:nil];
}

//移除消息
-(void)removeAllObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
